package com.gw2.gw2world.controller;

import java.util.List;

import com.gw2.gw2world.R;
import com.gw2.gw2world.model.Event;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Switch;

public class ConfigureEventAdapter extends ArrayAdapter<Event> implements OnCheckedChangeListener {
	@SuppressWarnings("unused")
	private static final String TAG="ConfigureEventAdapter";
	private DatabaseOpenHelper dbHelper;
	private SQLiteDatabase db;
	public ConfigureEventAdapter(Context c, List<Event> v) {
		super(c, android.R.layout.simple_list_item_1, v);
		dbHelper = new DatabaseOpenHelper(c, "database", null, 1);
		db=dbHelper.getWritableDatabase();
	}
	
	@Override
	  public View getView(int position, View convertView, ViewGroup parent) {
		LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View row = inflater.inflate(R.layout.event_configuration, parent, false);
		Switch sw_event=(Switch)row.findViewById(R.id.sw_event);
		sw_event.setChecked(getItem(position).enabled);
		sw_event.setOnCheckedChangeListener(this);
		sw_event.setTag(getItem(position));
		sw_event.setChecked(getItem(position).enabled);
		///set text with translation @string/xxx
		//android.util.Log.d(TAG,"ID: " + String.valueOf(context.getResources().getIdentifier(events.get(position).name, "string", context.getPackageName())) + " " +events.get(position).name);
		sw_event.setText(getContext().getResources().getIdentifier(getItem(position).name, "string", getContext().getPackageName()));
		return row;
	}

	@Override
	public void onCheckedChanged(CompoundButton view, boolean v) {
		Event e=(Event)view.getTag();
		e.enabled=v;
		ContentValues cv=new ContentValues();
		if(v) cv.put("repeat", 0);
		else cv.putNull("repeat");
		//android.util.Log.d(TAG, "ID: "+String.valueOf(e.id)+" - name: "+view.getText().toString() + ": "+String.valueOf(v));
		db.update("master_event", cv, "id=?", new String[]{String.valueOf(e.id)});
	}
}
